Skip to content

feat: show multi-threading in example #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 18, 2023
Merged

Conversation

ajewellamz
Copy link
Contributor

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ajewellamz ajewellamz requested a review from a team as a code owner July 10, 2023 15:25
Comment on lines 238 to 248
// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for(int i = 0; i < 20; ++i)
QueryItemWithCompoundBeacon(ddb, ddbTableName);
};
// increase once we expect threads to work
for(int i = 0; i < 1; ++i) {
Thread run = new Thread(myThread);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a thread pool would be cleaner here:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

...

MAX_CONCURRENT_QUERY_THREADS = 1;

Then,

Suggested change
// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for(int i = 0; i < 20; ++i)
QueryItemWithCompoundBeacon(ddb, ddbTableName);
};
// increase once we expect threads to work
for(int i = 0; i < 1; ++i) {
Thread run = new Thread(myThread);
}
}
// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for (int i = 0; i < 20; i++) {
QueryItemWithCompoundBeacon(ddb, ddbTableName);
}
};
ExecutorService pool = Executors.newFixedThreadPool(MAX_CONCURRENT_QUERY_THREADS);
for(int i = 0; i < MAX_CONCURRENT_QUERY_THREADS; i++) {
pool.execute(myThread);
}
pool.shutDown();

We could also manage threads individually, but I think we would have to manage them in some data structure to ensure all threads join before we return success from the test, e.g.

    Thread[] threads = new Thread[MAX_CONCURRENT_QUERY_THREADS];
    // If instead you were working in a multi-threaded context
    // it might look like this
    Runnable myThread = () -> {
        for(int i = 0; i < 20; i++)
            QueryItemWithCompoundBeacon(ddb, ddbTableName);
    };
    // increase once we expect threads to work
    for(int i = 0; i < MAX_CONCURRENT_QUERY_THREADS; i++) {
        Thread run = new Thread(myThread);
        threads[i] = run;
        run.start();
    }
    for(int i = 0; i < MAX_CONCURRENT_QUERY_THREADS; i++) {
        run.join()
    }

@ajewellamz ajewellamz merged commit f15dbe9 into main Jul 18, 2023
@ajewellamz ajewellamz deleted the add-threaded-example branch July 18, 2023 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants